package com.coderg.gmall.cart.controller;

import com.coderg.gmall.cart.service.CartService;
import com.coderg.gmall.common.constant.CartConst;
import com.coderg.gmall.common.result.Result;
import com.coderg.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author CoderG
 * @Date 2025/6/2 23:20
 **/
@RestController
@RequestMapping("/cart/info")
public class CartController {
    @Autowired
    CartService cartService;

    /**
     * 添加购物车
     *
     * @param skuId
     * @param goodsNumber
     */
    @GetMapping(value = "/addCart/{skuId}/{goodsNumber}")
    public Result addCart(@PathVariable Long skuId,
                          @PathVariable Long goodsNumber) {
        cartService.addCart(skuId, goodsNumber);
        return Result.ok();
    }

    /**
     * 查询购物车
     *
     * @return
     */
    @GetMapping(value = "/getCarts")
    public Result getCarts() {
        List<CartInfo> carts = cartService.getCarts();
        return Result.ok(carts);
    }


    /**
     * 修改购物车
     *
     * @param cartId
     * @param goodsNumber
     */
    @PutMapping(value = "/updateCart/{cartId}/{goodsNumber}")
    public Result updateCart(@PathVariable Long cartId,
                             @PathVariable Long goodsNumber) {
        cartService.updateCart(cartId, goodsNumber);
        return Result.ok();
    }

    /**
     * 删除购物车
     *
     * @param cartId
     */
    @DeleteMapping(value = "/deleteCart/{cartId}")
    public Result deleteCart(@PathVariable Long cartId) {
        cartService.deleteCart(cartId);
        return Result.ok();
    }

    /**
     * 选中
     *
     * @param cartId
     */
    @GetMapping(value = "/checkOn")
    public Result checkOn(@RequestParam("id") Long cartId) {
        cartService.checkOrNo(cartId, CartConst.CHECK_ON);
        return Result.ok();
    }

    /**
     * 选中或者取消选中，全选或者全不选
     *
     * @param cartId
     */
    @GetMapping(value = "/checkUp")
    public Result checkUp(@RequestParam("id") Long cartId) {
        cartService.checkOrNo(cartId, CartConst.CHECK_UP);
        return Result.ok();
    }

    /**
     * 查询确认界面
     *
     * @return
     */
    @GetMapping(value = "/getCartConfirm")
    public Result cartConfirm() {
        Map orderConfirmInfo = cartService.getOrderConfirmInfo();
        return Result.ok(orderConfirmInfo);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @GetMapping(value = "/clearCarts")
    public Boolean clearCarts() {
        try {
            return cartService.clearCarts();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
